/**
 * Copyright 2015 lerry
 * 
 * All right reserved.
 * 
 * Jan 7, 2015
 */
package com.bac.bacplatform;

import com.bac.utils.Config;

import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * @author lerry
 *
 */
public class BacAgreementWebActivity extends BaseActivity {
	
	private WebView mWebView;
	private boolean mIsError = false;
	private TextView mTVTitle;
	private ProgressBar mPb;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.coupon_agreement);
		
		mWebView = (WebView)findViewById(R.id.wb_yeepay);
		mWebView.setWebViewClient(new BacWebClient());
		mWebView.setWebChromeClient(new BacWebChromeClient());
		
		WebSettings webSettings = mWebView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		
		mTVTitle = (TextView)findViewById(R.id.web_title);
		
		mPb = (ProgressBar) findViewById(R.id.pb);
		mPb.setMax(100);
		
		String urlType = getIntent().getStringExtra("urltype");
		String loadUrl = "";
		if (urlType.equals("agree")) {
			loadUrl = Config.URL_COUPON_AGREE;
			mTVTitle.setText("协议详情");
		}else if(urlType.equals("detail")){
			loadUrl = Config.URL_COUPON_DETAIL;
			mTVTitle.setText("套餐详情");
		}else if (urlType.equals("about_us")) {
			loadUrl = Config.URL_ABOUT_US;
			mTVTitle.setText("关于我们");
		}else if(urlType.equals("ads_detail")){
			String adIdString = getIntent().getStringExtra("ads_id");
			loadUrl = Config.URL_HOME_ADS_DETAIL + "?id=" + adIdString;
			mTVTitle.setText("活动详情");
		}else if(urlType.equals("ads_local")){
			String urlString = getIntent().getStringExtra("ads_url");
			loadUrl = urlString;
			mTVTitle.setText("活动详情");
		}else{
			
		}
		mWebView.loadUrl(loadUrl);
		
		findViewById(R.id.title_left).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finishActivityOut();
			}
		});
	}

	@Override
	public void init() { 
	}
	
	private class BacWebChromeClient extends WebChromeClient {

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			mPb.setProgress(newProgress);
			if (newProgress == 100) {
				mPb.setVisibility(View.GONE);
			}
			super.onProgressChanged(view, newProgress);
		}
	}
	
	private class BacWebClient extends WebViewClient{
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}

		@Override
		public void onReceivedSslError(WebView view, SslErrorHandler handler,
				SslError error) {
			handler.proceed();
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			super.onReceivedError(view, errorCode, description, failingUrl);
			// 加载本地页面，进行出错处理
			mIsError = true;
			view.loadUrl("file:///android_asset/NetworkError.html");
		}
		
	}

	@Override
	protected void onStop() {
		super.onStop();
		mWebView.stopLoading();
	}
}
